<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/commons/taglib.jsp"%>
<%@ attribute name="id" type="java.lang.String" required="true" description="编号"%>
<%@ attribute name="name" type="java.lang.String" required="true" description="隐藏域名称（ID）"%>
<%@ attribute name="value" type="java.lang.String" required="true" description="隐藏域值（ID）"%>
<%@ attribute name="labelName" type="java.lang.String" required="true" description="输入框名称（Name）"%>
<%@ attribute name="labelValue" type="java.lang.String" required="true" description="输入框值（Name）"%>
<%@ attribute name="jsonRoot" type="java.lang.String" required="false" description="数据路径"%>
<%@ attribute name="parentIdKey" type="java.lang.String" required="false" description="选择框标题"%>
<%@ attribute name="title" type="java.lang.String" required="true" description="选择框标题"%>
<%@ attribute name="url" type="java.lang.String" required="true" description="树结构数据地址"%>
<%@ attribute name="checked" type="java.lang.Boolean" required="false" description="是否显示复选框，如果不需要返回父节点，请设置notAllowSelectParent为true"%>
<%@ attribute name="extId" type="java.lang.String" required="false" description="排除掉的编号（不能选择的编号）"%>
<%@ attribute name="isAll" type="java.lang.Boolean" required="false" description="是否列出全部数据，设置true则不进行数据权限过滤（目前仅对dept有效）"%>
<%@ attribute name="notAllowSelectRoot" type="java.lang.Boolean" required="false" description="不允许选择根节点"%>
<%@ attribute name="notAllowSelectParent" type="java.lang.Boolean" required="false" description="不允许选择父节点"%>
<%@ attribute name="allowClear" type="java.lang.Boolean" required="false" description="是否允许清除"%>
<%@ attribute name="allowInput" type="java.lang.Boolean" required="false" description="文本框可填写"%>
<%@ attribute name="cssClass" type="java.lang.String" required="false" description="css样式"%>
<%@ attribute name="cssStyle" type="java.lang.String" required="false" description="css样式"%>
<%@ attribute name="smallBtn" type="java.lang.Boolean" required="false" description="缩小按钮显示"%>
<%@ attribute name="hideBtn" type="java.lang.Boolean" required="false" description="是否显示按钮"%>
<%@ attribute name="disabled" type="java.lang.String" required="false" description="是否限制选择，如果限制，设置为disabled"%>
<%@ attribute name="dataMsgRequired" type="java.lang.String" required="false" description=""%>


<div class="input-group">
	<input id="${id}Id" name="${name}" class="${cssClass}" type="hidden" value="${value}"/>
	<input id="${id}Name" name="${labelName}" ${allowInput?'':'readonly="readonly"'} type="text" value="${labelValue}" data-msg-required="${dataMsgRequired}"
		class="form-control ${cssClass}" style="${cssStyle}"/>
		<span class="input-group-btn">
		<a id="${id}Button" href="javascript:" class="btn btn-primary btn-sm ${disabled} ${hideBtn ? 'hide' : ''}"  ${disabled}><i class="icon-search"></i></a>
		</span>
</div>

<!-- Modal -->
<div id="${id}Modal" class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">选择${title}</h4>
      </div>
      <div class="modal-body">
      	<sys:tree id="${id }" url="${url }" jsonRoot="${jsonRoot }" parentIdKey="${parentIdKey }"></sys:tree>
      </div>
      <div class="modal-footer">
    	 <button type="button" class="btn btn-primary btn-save" >保存</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
	$("#${id}Button, #${id}Name").click(function(){
		// 是否限制选择，如果限制，设置为disabled
		if ($("#${id}Button").hasClass("disabled")){
			return true;
		}
		/* $("#${id}Modal").modal({
			backdrop:false,
		}); */
		$("#${id}Modal").modal("show");
		$("#${id}Modal .modal-footer .btn-save").click(function(){
			$("#${id}Modal").modal("hide");
		});
		$('#${id}Modal').on('hidden.bs.modal', function (e) {
			
			//var tree = h.find("iframe")[0].contentWindow.tree;//h.find("iframe").contents();
			var tree =  window.${id}Tree;
			var ids = [], names = [], nodes = [];
			if ("${checked}" == "true"){
				nodes = tree.getCheckedNodes(true);
			}else{
				nodes = tree.getSelectedNodes();
			}
			for(var i=0; i<nodes.length; i++) {//<c:if test="${checked && notAllowSelectParent}">
				if (nodes[i].isParent){
					continue; // 如果为复选框选择，则过滤掉父节点
				}//</c:if><c:if test="${notAllowSelectRoot}">
				if (nodes[i].level == 0){
					alert("不能选择根节点（"+nodes[i].name+"）请重新选择。");
					return false;
				}//</c:if><c:if test="${notAllowSelectParent}">
				if (nodes[i].isParent){
					alert("不能选择父节点（"+nodes[i].name+"）请重新选择。");
					return false;
				}//</c:if><c:if test="${not empty module && selectScopeModule}">
				/* if (nodes[i].module == ""){
					alert("不能选择公共模型（"+nodes[i].name+"）请重新选择。");
					return false;
				}else if (nodes[i].module != "${module}"){
					alert("不能选择当前栏目以外的栏目模型，请重新选择。");
					return false;
				}//</c:if> */
				ids.push(nodes[i].id);
				names.push(nodes[i].name);//<c:if test="${!checked}">
				break; // 如果为非复选框选择，则返回第一个选择  </c:if>
			}
			$("#${id}Id").val(ids.join(",").replace(/u_/ig,""));
			$("#${id}Name").val(names.join(","));
		})
	});
	//@ sourceURL=tree_dialog_tag_${id}.js
</script>